package com.leonardo.persistenceme;

import java.util.Date;
import java.util.Hashtable;
import java.util.Vector;

import com.leonardo.beanme.model.ComboBox;
import com.leonardo.beanme.model.RadioBox;
import com.leonardo.persistenceme.model.persistancehandlers.BooleanArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.BooleanPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.ByteArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.BytePersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.CharArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.CharPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.DatePersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.DefaultPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.DoubleArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.DoublePersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.FloatArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.FloatPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.IntegerArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.IntegerPersistanceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.LongArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.LongPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.PersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.ShortArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.ShortPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.StringArrayPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.StringPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.VectorPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.OptionListPersistenceHandler.ComboBoxPersistenceHandler;
import com.leonardo.persistenceme.model.persistancehandlers.OptionListPersistenceHandler.RadioBoxPersistenceHandler;

public class Class2Handler {

	private static Class defaultHandlerClass = DefaultPersistenceHandler.class;
	
	private static Hashtable map = new Hashtable();
	
	static {
		map.put(Integer.class, IntegerPersistanceHandler.class);
		map.put(String.class, StringPersistenceHandler.class);
		map.put(Boolean.class, BooleanPersistenceHandler.class);
		map.put(Long.class, LongPersistenceHandler.class);
		map.put(Short.class, ShortPersistenceHandler.class);
		map.put(Byte.class, BytePersistenceHandler.class);
		map.put(Character.class, CharPersistenceHandler.class);
		map.put(Double.class, DoublePersistenceHandler.class);
		map.put(Float.class, FloatPersistenceHandler.class);
		map.put(Vector.class, VectorPersistenceHandler.class);
		map.put(Integer[].class, IntegerArrayPersistenceHandler.class);
		map.put(String[].class, StringArrayPersistenceHandler.class);
		map.put(Boolean[].class, BooleanArrayPersistenceHandler.class);
		map.put(Long[].class, LongArrayPersistenceHandler.class);
		map.put(Short[].class, ShortArrayPersistenceHandler.class);
		map.put(Byte[].class, ByteArrayPersistenceHandler.class);
		map.put(Character[].class, CharArrayPersistenceHandler.class);
		map.put(Double[].class, DoubleArrayPersistenceHandler.class);
		map.put(Float[].class, FloatArrayPersistenceHandler.class);
		map.put(Date.class, DatePersistenceHandler.class);
		map.put(ComboBox.class, ComboBoxPersistenceHandler.class);
		map.put(RadioBox.class, RadioBoxPersistenceHandler.class);
	}
	
	public static PersistenceHandler getHandler(Class clazz) {
		if(map.containsKey(clazz)) {
			try {
				return (PersistenceHandler) ((Class) map.get(clazz)).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		throw new IllegalArgumentException("Class " + clazz + " not supported");
	}
	
	public static PersistenceHandler getHandlerOrDefault(Class clazz) {
		try {
			return getHandler(clazz);
		} catch(IllegalArgumentException e) {
			return getDefaultHandlerInstance(); 
		}
	}
	
	public static void addHandler(Class key, PersistenceHandler handler) {
		map.put(key, handler.getClass());
	}
	
	public static PersistenceHandler getDefaultHandlerInstance() {
		try {
			return (PersistenceHandler) defaultHandlerClass.newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException(e.getMessage());
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	public static void setDefaultPersistenceHandler(PersistenceHandler handler) {
		defaultHandlerClass = handler.getClass();
	}
	
}
